import ReactDOM from "react-dom";
import NcTips from "./NcTips";

class NcTipsHandler {
  el = null;

  show(options = {}) {
    this.checkAndNewMountedDom();
    !!this.el &&
      ReactDOM.render(
        <NcTips
          close={() => this.hide()}
          confirm={(e) => {
            !!options.confirm && options.confirm(e);
            !options.isCloseHide && this.hide();
          }}
        />,
        this.el
      );
  }

  hide() {
    const dom = document.getElementById("numcode-tips");
    dom?.remove();
  }

  checkAndNewMountedDom() {
    let dom = document.getElementById("numcode-tips");
    if (!dom) {
      dom = document.createElement("div");
      dom.id = "numcode-tips";
      document.body.append(dom);
    }
    this.el = dom;
  }
}

export default new NcTipsHandler();
